package com.linxz.wandnroid.presenter;

import com.linxz.framework.base.ActivityLifeCycleEvent;
import com.linxz.framework.base.presenter.BasePresenter;
import com.linxz.framework.protocol.callback.ModelCallBack;
import com.linxz.wandnroid.appui.view.ProjectListView;
import com.linxz.wandnroid.module.WanAndroidModule;
import com.linxz.wandnroid.pojo.WandroidProjectBean;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.subjects.PublishSubject;

/**
 * @author Linxz
 * 创建日期：2020年02月29日 10:21
 * version：v4.5.4
 * 描述：
 */
public class ProjectListPresenter extends BasePresenter<ProjectListView, WanAndroidModule> {
    private int cid;
    private int page=0;
    private List<WandroidProjectBean> projectBeans=new ArrayList<>();

    public List<WandroidProjectBean> getProjectBeans() {
        return projectBeans;
    }

    public ProjectListPresenter(ProjectListView mView, PublishSubject<ActivityLifeCycleEvent> lifecycleSubject) {
        super(mView, lifecycleSubject);
    }

    @Override
    public WanAndroidModule createModel(PublishSubject<ActivityLifeCycleEvent> lifecycleSubject) {
        return new WanAndroidModule(lifecycleSubject);
    }

    public void initCid(int cid){
        this.cid=cid;
    }

    public void refreshData(){
        mModel.getProjectList(cid, 0, new ModelCallBack<List<WandroidProjectBean>>() {
            @Override
            public void onSuccess(List<WandroidProjectBean> model) {
                if (model==null || model.size()==0){
                    mView.onLoadEmpy(projectBeans.size()==0);
                }else{
                    page=1;
                    projectBeans.clear();
                    projectBeans.addAll(model);
                    mView.onLoadSuccess();
                }
            }

            @Override
            public void onErrors(String code, String msg) {
                mView.onLoadError(msg,projectBeans.size()==0);
            }
        });
    }

    public void loadMoreData(){
        mModel.getProjectList(cid, page, new ModelCallBack<List<WandroidProjectBean>>() {
            @Override
            public void onSuccess(List<WandroidProjectBean> model) {
                if (model==null || model.size()==0){
                    mView.onLoadEmpy(projectBeans.size()==0);
                }else{
                    page++;
                    projectBeans.addAll(model);
                    mView.onLoadSuccess();
                }
            }

            @Override
            public void onErrors(String code, String msg) {
                mView.onLoadError(msg,projectBeans.size()==0);
            }
        });
    }
}
